package com.example.myapplication.ArrayAdapterInclude;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.AdapterActivity;
import com.example.myapplication.DataBase.PersonDataBase;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import java.util.ArrayList;

public class IntentArrayListAdapter extends ArrayAdapter<Person> {

  private ArrayList<Person> listData;
  private PersonDataBase personDataHelper;
  int convertViewId=-1;
  public IntentArrayListAdapter(Context context,ArrayList<Person> data) {
    super(context, R.layout.intent_text_info_layout,data);
    listData=data;

  }
  public IntentArrayListAdapter(Context context,ArrayList<Person> data,PersonDataBase dataHelper) {
    super(context, R.layout.intent_text_info_layout,data);
    listData=data;
    personDataHelper=dataHelper;
  }
  @SuppressLint("ResourceType")
  public IntentArrayListAdapter(Context context,int layoutResourceId,ArrayList<Person> data) {
    super(context, layoutResourceId,data);
    listData=data;
    convertViewId=layoutResourceId;
  }




  @NonNull @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertViewId != -1) {
      convertView = LayoutInflater.from(getContext()).inflate(convertViewId, parent, false);
    }
    if (convertView == null) {

      convertView = LayoutInflater.from(getContext())
          .inflate(R.layout.intent_text_info_layout, parent, false);

    }
    convertView.setOnLongClickListener(new View.OnLongClickListener() {
      @Override public boolean onLongClick(View v) {
        ShowPopMenu(v,position);
        return true;
      }
    });
    String str=ConvertText(getItem(position));
    TextView textView = (TextView) convertView.findViewById(R.id.infoView);
    textView.setText(str);
    return convertView;
  }

  private void ShowPopMenu(View view,final int position) {

    PopupMenu popupMenu = new PopupMenu(getContext(), view);
    popupMenu.getMenuInflater().inflate(R.menu.menu_popup, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.delete_data) {
          DeleteItem(position);
          return true;
        }
        return false;
      }
    });

    popupMenu.show();

  }

  private String ConvertText(Person person) {
    String str = getContext().getString(R.string.person_info, person.getName(), person.getJob());
    return str;
  }
  private void DeleteItem(final int position) {

    personDataHelper.open();
    int num=personDataHelper.DeleteSinglePerson(listData.get(position).getName());
    if (num == 1) {
      listData.remove(position);
      notifyDataSetChanged();

    } else if (num == 0) {
      System.out.println("没有这个人的信息");
    } else {
      System.out.println("删除出现了问题");
    }
    personDataHelper.close();


  }
}
